package com.qingke.Qiximybatis.business;

import org.apache.ibatis.session.SqlSession;
import com.qingke.Qiximybatis.QixiApp;
import com.qingke.Qiximybatis.QixiConsole;
import com.qingke.Qiximybatis.QixiDao;
import com.qingke.Qiximybatis.command.SystemCommand;
import com.qingke.Qiximybatis.mapper.MyUserMapper;
import com.qingke.Qiximybatis.sqlclass.MyUser;

public class Login extends SystemCommand{

	
	@Override
	public void execute() {
		SqlSession session = QixiDao.mybatisSession();
		MyUser user = new MyUser();
		String post = checkPostname();
		String pad = checkPassword();
		user.setId(user.getId());
		user.setPhone(post);
		user.setUsername(pad);
		
		MyUserMapper m = session.getMapper(MyUserMapper.class);
		MyUser name = m.getname(user);
		System.out.println("欢迎回来，" + name.getName());

		MyUserMapper userM = session.getMapper(MyUserMapper.class);
		MyUser userId = userM.getphone(post);
		QixiApp.getInstance().setUser(userId);
		System.err.println(userId);
		
	}

	public static String checkPostname() {
		SqlSession session = QixiDao.mybatisSession();
		String  postname;
		while(true){
			postname = QixiConsole.askUserInput("输入你的电话号码:>");
			MyUserMapper usermap = session.getMapper(MyUserMapper.class);
			MyUser phone = usermap.getphone(postname);
			if (phone.getPhone()!=null) {
				System.out.println("账号正确！");
				break;
			} else {
				QixiConsole.askUserInput("账号不正确！请重新输入！");
			}
		}
		return postname;
	}

	public static String checkPassword() {
		SqlSession session = QixiDao.mybatisSession();
		String username ;
		while(true){
			username = QixiConsole.askUserInput("输入你的密码:>");
			MyUserMapper usermap = session.getMapper(MyUserMapper.class);
			MyUser user = usermap.getusername(username);
			if (user.getUsername()!=null) {
				break;
			} else {
				QixiConsole.askUserInput("密码不正确！请重新输入！");
			}
		}	
		System.out.println("密码正确！");
		return username;
	}
	public static MyUser user(String post,String pad){
		
		return null;
		
	}

}


